package GUI;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import validator.JStringValidator;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JPasswordField;

import Action.DatabaseConnection;
import Action.GlobalParam;

public class Login {

	private JFrame loginFrame;
	private JTextField tf_Account;
	private JStringValidator lb_Validator;
	private JPasswordField tf_Password;
	private Connection con = null;
	private PreparedStatement preSta = null;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Login window = new Login();
					window.loginFrame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Login() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		loginFrame = new JFrame();
		loginFrame.setTitle("Đăng nhập");
		loginFrame.setBounds(100, 100, 319, 240);
		loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		loginFrame.getContentPane().setLayout(null);
		
		JLabel lblNewLabel = new JLabel("BẢNG ĐĂNG NHẬP");
		lblNewLabel.setForeground(new Color(0, 100, 0));
		lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD, 22));
		lblNewLabel.setBounds(56, 11, 210, 32);
		loginFrame.getContentPane().add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("Tài khoản");
		lblNewLabel_1.setBounds(20, 71, 57, 14);
		loginFrame.getContentPane().add(lblNewLabel_1);
		
		JLabel lblMtKhu = new JLabel("Mật khẩu");
		lblMtKhu.setBounds(20, 108, 57, 14);
		loginFrame.getContentPane().add(lblMtKhu);
		
		tf_Account = new JTextField();
		tf_Account.setBounds(87, 68, 162, 20);
		loginFrame.getContentPane().add(tf_Account);
		tf_Account.setColumns(10);
		
		JButton bt_Login = new JButton("Đăng nhập");
		bt_Login.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					bt_LoginActionPerformed(arg0);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		bt_Login.setBounds(107, 136, 99, 23);
		loginFrame.getContentPane().add(bt_Login);
		
		JLabel lb_Config = new JLabel("Tùy chỉnh dữ liệu");
		lb_Config.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseMoved(MouseEvent arg0) {
				//
			}
		});
		lb_Config.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				lb_ConfigMouseClick(arg0);
			}
		});
		lb_Config.setBounds(198, 181, 108, 14);
		loginFrame.getContentPane().add(lb_Config);
		
		lb_Validator = new JStringValidator();
		lb_Validator.setForeground(new Color(255, 0, 0));
		lb_Validator.setFont(new Font("Tahoma", Font.ITALIC, 11));
		lb_Validator.setBounds(56, 46, 208, 14);
		loginFrame.getContentPane().add(lb_Validator);
		
		tf_Password = new JPasswordField();
		tf_Password.setBounds(87, 105, 162, 20);
		loginFrame.getContentPane().add(tf_Password);
		tf_Account.setText("admin");
		tf_Password.setText("admin");
	}

	private void bt_LoginActionPerformed(ActionEvent arg0) throws Exception
	{
		lb_Validator.setLabelFor(tf_Account);
		if(!lb_Validator.check())
		{
			return;
		}
		lb_Validator.setLabelFor(tf_Password);
		if(!lb_Validator.check())
		{
            return;
		}
		DatabaseConnection connect = new DatabaseConnection();
		if(connect.LoadConfig())
		{
			con = connect.Connect();
			if(con!=null)
			{
				GlobalParam.con = con;
				preSta = con.prepareStatement("SELECT * FROM Users WHERE Username=? AND Password=?");
				preSta.setString(1, tf_Account.getText().trim());
				preSta.setString(2, String.valueOf(tf_Password.getPassword()));
				ResultSet rs = preSta.executeQuery();
				if(rs.next()){
					//
					System.out.println("OK");
				}
				else{
		            lb_Validator.setText("Đăng nhập thất bại! Sai mật khẩu hoặc tài khoản.");
		            tf_Password.setText("");
		            tf_Account.grabFocus();
				}
			}
			else
			{
	            JOptionPane.showMessageDialog(null, "Không thể truy cập vào cơ sở dữ liệu!\nHãy kiểm tra lại tệp tùy chỉnh...", "Lỗi", JOptionPane.ERROR_MESSAGE);
	            new Config().setVisible(true);
			}
		}
		else
		{
            JOptionPane.showMessageDialog(null, "Không tìm thấy tệp tùy chỉnh!\nBạn phải nhập nội dung vào tệp để tiếp tục...", "Lỗi", JOptionPane.ERROR_MESSAGE);
            new Config().setVisible(true);
		}
		
	}
	private void lb_ConfigMouseClick(MouseEvent arg0)
	{
		new Config().setVisible(true);
	}
}
